#include "WPH_server.h"
#include "sql.h"
#include "account.h"


//接收服务器传过来的数据，把管理员信息插入数据库表中
void mysqlInfo(struct regulator info,char*buf)
{
   	MYSQL *mysql = NULL;
    MYSQL *res = NULL;
    char sql[100] = {0};
    char type[30] = {0};

    strcpy(type,funType(buf,type));//取出type

    LOG_D("type = %s\n",type);
    LOG_D("%d  %s  %s  %d  %d  %d",info.id,info.name,info.gender,info.age,info.account,info.cipher);

    mysql = mysql_init(NULL);//初始化

    res = mysql_real_connect(mysql,"localhost","","","water",0,NULL,0);
    if(res == NULL)
    {
        LOG_E("%s",mysql_error(mysql));
        mysql_close(mysql);
         return ;
    }
    printf("connect success!\n");

    if(strcmp(type,"Administratorregistration") == 0)
    {
        sprintf(sql,"insert into adm values(%d,'%s','%s',%d,%d,%d)",info.id,info.name,info.gender,info.age,info.account,info.cipher);
        int ret = 0;
        ret = mysql_query(mysql,sql);
        if(ret != 0)
        {
            LOG_E("%s\n",mysql_error(mysql));
            mysql_close(mysql);
             return ;
        }
        printf("插入成功！\n");
    }
    mysql_close(mysql);
}
//把员工信息插入数据库表中
void mysqlInfoStaff(struct staff staff_info,char*buf)
{
   	MYSQL *mysql = NULL;
    MYSQL *res = NULL;
    char sql[100] = {0};
    char type[30] = {0};
    strcpy(type,funType(buf,type));//取出type

    LOG_D("type = %s\n",type);
    LOG_D("%d  %s  %s  %d  %d  %d",staff_info.uid,staff_info.name,staff_info.gender,staff_info.age,staff_info.account,staff_info.cipher);

    mysql = mysql_init(NULL);//初始化

    res = mysql_real_connect(mysql,"localhost","","","water",0,NULL,0);
    if(res == NULL)
    {
        LOG_E("%s",mysql_error(mysql));
        mysql_close(mysql);
        return ;
    }
    printf("connect success!\n");

    if(strcmp(type ,"Employeeregistration") == 0)
    {
        sprintf(sql,"insert into staff values(%d,'%s','%s',%d,%d,%d)",staff_info.uid,staff_info.name,staff_info.gender,staff_info.age,staff_info.account,staff_info.cipher);
        printf("插入成功！\n");
    }

    int ret = 0;
    ret = mysql_query(mysql,sql);
    if(ret != 0)
    {
        LOG_E("%s\n",mysql_error(mysql));
        mysql_close(mysql);
        return ;
    }
    mysql_close(mysql);
}

//读取管理员表中信息
struct reg* readMysqlInfo()
{
    MYSQL *mysql = NULL;
    MYSQL *rs = NULL;
	char sql[100] = {"select * from adm"};

	mysql = mysql_init(NULL);

	rs = mysql_real_connect(mysql,"localhost","","","water",0,NULL,0);
	if(rs == NULL)
	{
		LOG_D("%s\n",mysql_error(mysql));
		mysql_close(mysql);
		exit(-1);
	}

	int ret = 0;
	ret = mysql_query(mysql,sql);
	if(ret != 0)
	{
		printf("%s\n",mysql_error(mysql));
		mysql_close(mysql);
		exit(-1);
	}

	MYSQL_RES *res = NULL;
	MYSQL_ROW row = NULL;

	res = mysql_store_result(mysql);

	int nums = 0;
    int ct = 0;
    int i = 0;
    int j = 0;
    struct reg *head = NULL;
    head = (struct reg*)malloc(sizeof(struct reg));
    memset(head,0,sizeof(struct reg));

    struct reg* cur = head;
    struct reg* node = NULL;

    row = mysql_fetch_row(res);
	while(row != NULL)
	{
        node = (struct reg*)malloc(sizeof(struct reg));
        memset(node,0,sizeof(struct reg));

		node->id = atoi(row[0]);
        strcpy(node->name,row[1]);
        strcpy(node->gender,row[2]);
        node->age = atof(row[3]);
        node->account = atof(row[4]);
        node->cipher = atof(row[5]);

        node->next = NULL;
        cur->next = node;
        cur = cur->next;
		row = mysql_fetch_row(res);
	}

	mysql_free_result(res);
	mysql_close(mysql);

    return head;
}

//读取员工表中信息
struct sta* readInfo()
{
    MYSQL *mysql = NULL;
    MYSQL *rs = NULL;
	char sql[100] = {"select * from staff"};

	mysql = mysql_init(NULL);

	rs = mysql_real_connect(mysql,"localhost","","","water",0,NULL,0);
	if(rs == NULL)
	{
		LOG_E("%s\n",mysql_error(mysql));
		mysql_close(mysql);
		exit(-1);
	}

	int ret = 0;
	ret = mysql_query(mysql,sql);
	if(ret != 0)
	{
		LOG_E("%s\n",mysql_error(mysql));
		mysql_close(mysql);
		exit(-1);
	}

	MYSQL_RES *res = NULL;
	MYSQL_ROW row = NULL;

	res = mysql_store_result(mysql);

	int nums = 0;
    int ct = 0;
    int i = 0;
    int j = 0;
    struct sta *head = NULL;
    head = (struct sta*)malloc(sizeof(struct sta));
    memset(head,0,sizeof(struct sta));

    struct sta* cur = head;
    struct sta* node = NULL;

    row = mysql_fetch_row(res);
	while(row != NULL)
	{
        node = (struct sta*)malloc(sizeof(struct sta));
        memset(node,0,sizeof(struct sta));

		node->uid = atoi(row[0]);
        strcpy(node->name,row[1]);
        strcpy(node->gender,row[2]);
        node->age = atof(row[3]);
        node->account = atof(row[4]);
        node->cipher = atof(row[5]);

        node->next = NULL;
        cur->next = node;
        cur = cur->next;
		row = mysql_fetch_row(res);
	}

	mysql_free_result(res);
	mysql_close(mysql);

    return head;
}